<?php
/**
 * Created by PhpStorm.
 * User: peter
 * Date: 2018/5/28
 * Time: 下午5:31
 */

namespace app\activity\controller;


use extend\Functions;
use extend\STATUS_CODE;
use think\App;
use think\Controller;
use think\facade\Request;

class Base extends Controller
{

    public static $token = '', $client_type = '';

    /**
     * Base constructor.
     * @param App|null $app
     * @throws \Exception
     */
    public function __construct(App $app = null)
    {
        parent::__construct($app);
        self::$token       = Request::header('token');
        self::$client_type = Request::header('client-type');
        self::checkClientType();
    }

    /**
     * @purpose 检测当前客户端是否是合法的请求
     * @throws \Exception
     */
    private static function checkClientType()
    {
        if (!self::$client_type) {
            exception(
                'Error data...!',
                STATUS_CODE::PARAMETER_ERROR);
        }
        // 当前请求只能由微信公众号客户端发起
        if (!in_array(self::$client_type, ['wechat', 'app', 'h5', 'pc'])) {
            exception(
                'Error data...!',
                STATUS_CODE::PARAMETER_ERROR);
        }
    }

    /**
     * @purpose 返回数据
     * @param int|mixed $code
     * @param array $data
     * @param string $message
     * @return array
     */
    public function response($code = STATUS_CODE::SUCCESS, $data = [], $message = 'success'): array
    {
        return Functions::result($code, $data, $message);
    }
}